package com.lckj.android.shell.module.main;

import android.Manifest;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.PermissionUtils;
import com.lckj.android.shell.R;
import com.lckj.android.shell.base.BaseActivity;
import com.lckj.android.shell.module.web.WebActivity;
import com.lckj.android.shell.utils.PreferencesUtils;


/**
 * @author lc. 2023-10-22 15:16
 */
public class MainActivity extends BaseActivity {

    private final String[] mPermissions = new String[]{Manifest.permission.CAMERA};
    private EditText mUrlInput;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.module_main_activity);

        initView();
    }

    private void initView() {
        mUrlInput = findViewById(R.id.et_url);
        mUrlInput.setText(PreferencesUtils.getLastUrl());

        findViewById(R.id.btn_open).setOnClickListener(view -> {
            String url = mUrlInput.getText().toString();
            if (TextUtils.isEmpty(url)) {
                showToast("请输入链接");
                return;
            }

            PreferencesUtils.setLastUrl(url);
            PermissionUtils.permission(mPermissions).callback(new PermissionUtils.SimpleCallback() {
                @Override
                public void onGranted() {
                    WebActivity.open(MainActivity.this, "", url);
                }

                @Override
                public void onDenied() {
                    showToast("请授权使用相机");
                    WebActivity.open(MainActivity.this, "", url);
                }
            }).request();
        });
    }

    private void requestPermissionAndGo() {
        PermissionUtils.permission(mPermissions).callback(new PermissionUtils.SimpleCallback() {
            @Override
            public void onGranted() {

            }

            @Override
            public void onDenied() {

            }
        }).request();
    }

    private void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}
